import Vue from 'vue'
import util from '../router/util';
import Router from 'vue-router'
import routers from './model/router';
import store from '../state/index';
import iView from 'iview';
Vue.use(Router);

// 路由配置
const RouterConfig = {
  // mode: 'history',
  routes: routers
};
const router = new Router(RouterConfig);
router.beforeEach((to, form, next) => {
  console.log(to.name);
  iView.LoadingBar.start();
  let user = localStorage.getItem('userName');
  if (to.name !== 'login' && user === null) {
    next({
      name: 'login'
    });
  } else if (to.name === 'login' && user !== null) {
      next({
        name: 'home'
      });
  } else if (to.name === null){
      next({
        name: 'error404'
      });
  } else {
    next();
  }

});

router.afterEach(route => {
  iView.LoadingBar.finish();
});

export default router;


